﻿
var isfirst=true;
var kwdefault="输入产品描述或元器件型号";
var searchtime;//搜索时间控制
var searchwaittime;//搜索等待时间
var urlhead="/";

jQuery(document).ready(function () {

    htmlkwlink=jQuery("#historysearchkw").html();       //将“或者尝试”后的建议搜索关键词内容赋给htmlwlink
    jQuery("#searchBox").bind("keyup",function(event) {
        isfirst=false;
        //空格32
        jQuery(".searchtxt").css("color","#333");
        if((event.keyCode>=112 && event.keyCode<=137)|| (event.keyCode>=12 && event.keyCode<=31)|| (event.keyCode>=33 && event.keyCode<=40)|| event.keyCode==9 || event.altKey ||
            event.shiftKey ){

        } else{
            if(searchtime!="undefined"){
                clearTimeout(searchtime);
            }
            searchchange();
        }
    });

    jQuery("body").bind("keyup",function(event) {
        var _v=arguments.callee.caller.arguments[0] || window.event;
        if(_v.keyCode==13){
            btnsearch();
        }
    });

    /* 滚动 */

    var addr = new Array;
    jQuery(".alldiv").map(function() {
        addr.push(jQuery(this).offset().top);
    });

    jQuery(".navbar").css("display","none");
//       if(jQuery(window).height()>=addr[4]) {
//           jQuery(".fankuibg,.jifen,.gongneng,.jieshao").addClass("zoom");
//       } else if(jQuery(window).height()>=addr[3]) {
//           jQuery(".jifen,.gongneng,.jieshao").addClass("zoom");
//       } else if(jQuery(window).height()>=addr[2]) {
//           jQuery(".gongneng,.jieshao").addClass("zoom");
//       } else{
//           if(jQuery(window).height()>=addr[1]) {
//               jQuery(".jieshao").addClass("zoom");
//           }
//       }
    if(jQuery(window).height()>=addr[5]) {
        jQuery(".fankuibg,.jifen,.gongneng,.yingbaozhi,.jieshao").addClass("zoom");
    } else if(jQuery(window).height()>=addr[4]) {
        jQuery(".jifen,.gongneng,.yingbaozhi,.jieshao").addClass("zoom");
    } else if(jQuery(window).height()>=addr[3]) {
        jQuery(".gongneng,.yingbaozhi,.jieshao").addClass("zoom");
    } else if(jQuery(window).height()>=addr[2]) {
        jQuery(".yingbaozhi,.jieshao").addClass("zoom");
    } else{
        if(jQuery(window).height()>=addr[1]) {
            jQuery(".jieshao").addClass("zoom");
        }
    }

    //滑动屏幕时，功能介绍块放大展示
    jQuery(window).scroll(function() {

        var sT = window.pageYOffset;
        if(sT < addr[0]) {
            jQuery(".navbar").fadeOut();
        } else {
            jQuery(".navbar").fadeIn();
        }
        var viewH =sT + jQuery(window).height();
//        alert(viewH);
        if(viewH >= addr[1]) {
            jQuery(".jieshao").addClass("zoom");
        } else {
            jQuery(".jieshao,.yingbaozhi,.gongneng,.jifen,.fankuibg").removeClass("zoom");
        }
        if(viewH >= addr[2]) {
            jQuery(".yingbaozhi").addClass("zoom");
        } else {
            jQuery(".yingbaozhi,.gongneng,.jifen,.fankuibg").removeClass("zoom")
        }
        if(viewH >= addr[3]) {
            jQuery(".gongneng").addClass("zoom");
        } else {
            jQuery(".gongneng,.jifen,.fankuibg").removeClass("zoom")
        }
        if(viewH >= addr[4]) {
            jQuery(".jifen").addClass("zoom");
        } else {
            jQuery(".jifen,.fankuibg").removeClass("zoom")
        }
        if(viewH >= addr[5]) {
            jQuery(".fankuibg").addClass("zoom");
        } else {
            jQuery(".fankuibg").removeClass("zoom");
        }

    });

    $("#searchIndex").click(function(){                    //点击搜索框弹出搜索页
        //jQuery("html, body").animate({scrollTop:"80%"});
        $("body").css("overflow-y","hidden");
        $(".hiddenDiv").css("display","none");
        $(".searchFixed").css("display","block");
        $(".searchFixed #searchBox").focus();
    });
//       $(".searchFixed .navbar-left2").click(function(){    //返回按钮
//           $("body").css("overflow-y","scroll");
//           $(".searchFixed").css("display","none");
//       })


});
function searchchange(){
    var kw=jQuery.trim(ToCDB(jQuery("#searchBox").val()));
    if(jQuery.trim(ToCDB(jQuery("#searchBox").val())).indexOf(ToCDB(kwdefault))>-1){
        var kw=jQuery.trim(ToCDB(jQuery("#searchBox").val())).replace(ToCDB(kwdefault),"");
        if(kw.length>0){
            jQuery("#searchBox").val(kw);
        }else{
            //解决输入头部空格问题
            if(ToCDB(jQuery("#searchBox").val())!=ToCDB(kwdefault)){
                jQuery("#searchBox").val("");
            }
        }
    }
    if(kw!=ToCDB(kwdefault)){
        if(searchtime!="undefined"){
            clearTimeout(searchtime);
        }
        searchtime=setTimeout(function(){searchic_suggest(kw);},300);
    }
}
//搜索
function searchic_suggest(kw) {

    if(searchtime!="undefined"){
        clearTimeout(searchtime);
    }
    kw=ToCDB(jQuery.trim(kw));

    if(kw.length>0){
        //jQuery("#historysearchkw").css("display","none");
        jQuery("#historysearchkw").html("<p style='height:14px;'> </p>");
    }else{
        //jQuery("#historysearchkw").css("display","block");
        jQuery("#historysearchkw").html(htmlkwlink);
    }
    if(kw.length>0){
        //旋转等待搜索结果

        searchwaittime=setTimeout(function(){searchwaitgif();},100);

        var ajaxdate="s=" + encodeURIComponent(kw)+"&date=" + new Date().getTime();

        jQuery.ajax({
            type: "get",
            url: urlhead+"m/easydatasheetapi/search/suggest",
            data:ajaxdate,
            dataType: "json",
            success: function(msg) {
                if(jQuery.trim(ToCDB(jQuery("#searchBox").val()))==kw || msg.type=="icpara"){
                    ajaxshow(kw,msg);
                }
                //停止旋转
                if(searchwaittime!="undefined"){
                    clearTimeout(searchwaittime);
                }
                if(jQuery("#searchwaiting").css("display")=="block"){
                    jQuery("#searchwaiting").css("display","none");
                    jQuery("#searchwaitok").css("display","block");
                }
            },
            error:function(XMLHttpRequest,textStatus,errorThrown){
                //alert(XMLHttpRequest.status);
                //停止旋转
                if(searchwaittime!="undefined"){
                    clearTimeout(searchwaittime);
                }
                if(jQuery("#searchwaiting").css("display")=="block"){
                    jQuery("#searchwaiting").css("display","none");
                    jQuery("#searchwaitok").css("display","block");
                }
            }
        });
    }else{
        //没有关键字，清除结果
        jQuery("#searchresults").html("");
    }
}

//加载结果
function ajaxshow(skw,results){
    jQuery("#searchresults").html("");
    if((results.type=="icpara" && results.count>0)){
        var type=results.type;
        jQuery("#searchresults").html(showresults(type,results));
    }
}
function searchwaitgif(){
    if(jQuery("#searchwaiting").css("display")=="none"){
        jQuery("#searchwaitok").css("display","none");
        jQuery("#searchwaiting").css("display","block");
    }
}
function showresults(resulttype,resultall){

    var results=resultall.data;
    var total=resultall.count;

    //param show
    var parastr=new StringBuffer();

    var _kw=ToCDB(jQuery.trim(jQuery("#searchBox").val()));
    var _kwlength=_kw.length;

    var noarray=new Array();
    var noarrayeach=new Array();
    for(var i=0;i<results.length;i++){
        if(jQuery.inArray(results[i].no.toLocaleUpperCase(),noarrayeach)==-1){
            noarrayeach.push(results[i].no.toLocaleUpperCase());
        }else{
            if(jQuery.inArray(results[i].no.toLocaleUpperCase(),noarray)==-1){
                noarray.push(results[i].no.toLocaleUpperCase());
            }
        }
    }

    var str=new StringBuffer();
    str.append("<div class=\"searchpadding\">");
    str.append("</div>");
    str.append("<div class=\"searchtop\">");
    for(var i=0;i<results.length;i++){
        str.append(showeachrow(results,i,resulttype,noarray,_kw,_kwlength,total));
    }
    str.append("</div>");
//        str.append("<div class=\"searchbottom\">");
//        str.append(" <div class=\"searchbottomleft\">"+jQuery.format(total, 3, ',')+"个产品型号</div>");
//        str.append(" <a href='javascript:btnsearch();'><div class=\"searchbottomright\">更多<span><img src='"+webpath+"/img_mo/index/jiantou.png'/></span></div></a>");
//        str.append("</div>");

    return str.toString();
};
function showeachrow(results,i,resulttype,noarray,_kw,_kwlength,total){
    var str=new StringBuffer();

    var _ishave=1;
    if(jQuery.inArray(results[i].no.toLocaleUpperCase(),noarray)==-1){
        _ishave=0;
    }

    var _manuShow=results[i].manu_r.toLocaleUpperCase();


    //list unclassifed  ,list Of unclassified
    if(_manuShow.indexOf("unclassified".toLocaleUpperCase())==-1 && _manuShow.indexOf("unclassifed".toLocaleUpperCase())==-1){
        var _manuShowi=_manuShow.indexOf(" ");
        if(_manuShowi>0){
            _manuShow=_manuShow.substring(0, _manuShowi);
        }

        if(_manuShow.length>12)
            _manuShow=_manuShow.substr(0,12)+"...";

    }else{
        _manuShow=START_EASYIC_SHOWRESULTS_INFO1;
    }

    var showno=indexnooper(results[i].no,_kw,_kwlength).toLocaleUpperCase();

    var _a_title="";
    var _tagnameClass="";//tagname的class
    if(resulttype=="cntagname"){
        _a_title=results[i].miaoshuen;
        _tagnameClass=" resultname";
    }else if(resulttype=="entagname"){
        _a_title=results[i].miaoshu;
        _tagnameClass=" resultname";
    }else{
        if(results.length<=5 || total<=5){
            _a_title=results[i].miaoshuen;
            if(_a_title==null || _a_title.length==0){
                _a_title=results[i].miaoshu;
            }
        }else{

            _a_title=results[i].miaoshu;
            if(_a_title==null || _a_title.length==0){
                _a_title=results[i].miaoshuen;
            }
        }
    }
    //首页搜索下拉框
    str.append("<ul id='endownpdf_"+i+"' title='"+_a_title+"' onclick='opendetail(\""+results[i].manu_r.toLocaleUpperCase()+"\",\""+results[i].no+"\");'>");
    str.append("<li class=\"searchleft\"><a href='javascript:void(0)'>"+showno+"</a></li>");
    //down
//        str.append("<li class=\"searchright\"><a  href='javascript:downpdf(\""+results[i].pdfUrl+"\","+i+",\""+results[i].manu_r.toLocaleUpperCase()+"\",\""+results[i].no
//                +"\");'><span></span><img src='"+webpath+"/img_mo/index/download.png'/></a></li>");

    str.append("</ul>");


    return str.toString();
}
function btnsearch(){
    var _kw=ToCDB(jQuery.trim(jQuery("#searchBox").val()));
    if(_kw!=ToCDB(kwdefault)){
        if(_kw.length>0){
//            if(_kw.indexOf('/')==-1){
//                window.open("/search/"+_kw);
//            }else{
//                window.open("/search?s="+_kw);
//            }
            if(_kw.indexOf('/')==-1){
                window.location.href="/m/search/"+_kw;
            }else{
                window.location.href="/m/search?s="+_kw;
            }
        }
    }
}

function indexnooper(no,_kw,_kwlength){
    if(isHanzi(_kw)){
        return no;
    }
    var _replacestr=indexnooper2(no,_kw,_kwlength);
    if(_replacestr.toUpperCase()==no.toUpperCase()){
        if(_kw.indexOf('-')>-1){
            _kw=_kw.replace("-","");
            return  indexnooper2(no,_kw,_kw.length);
        }
    }
    return _replacestr;
}
function indexnooper2(no,_kw,_kwlength){
    if(isHanzi(_kw)){
        return no;
    }
    var showno=no;
    var _no=no.substring(0,_kwlength);
    if(_kwlength>5 && _no.toLocaleUpperCase()==_kw.toLocaleUpperCase())
        showno=_kw+"<b>"+no.substring(_kwlength)+"</b>";
    else{
        var _start=showno.toLocaleUpperCase().indexOf(_kw.toLocaleUpperCase());
        if(_start>0){
            var _ststr="<span style='color:#8cc5ff'>"+no.substring(0,_start)+"</span>";
            var _enstr="<b>"+no.substring(_start+_kwlength)+"</b>";
            showno=_ststr+_kw+_enstr;
        }else{
            var _swz=-1;//开始位置
            var _ewz=-1;//结束位置
            var _kwz=0;//关键字为位置
            var _swzsize=showno.length;
            for(var _wzi=0;_wzi<_swzsize;_wzi++){
                var _wz=showno.toLocaleUpperCase().substr(_wzi,1);
                if(checkchar(_wz)){
                    if(_wz==_kw.toLocaleUpperCase().substr(_kwz,1)){
                        if(_kwz==0){
                            _swz=_wzi;
                        }
                        _kwz++;
                        if(_kwz==_kw.length){
                            _ewz=_wzi;
                            break;
                        }
                    }else{
                        _swz=-1;
                        _ewz=-1;
                        _kwz=0;
                    }
                }
            }
            if(_swz!=-1 && _ewz!=-1 ){
                var _ststr="<span style='color:#8cc5ff'>"+no.substring(0,_swz)+"</span>";
                var _cstr=no.substring(_swz,_ewz+1);
                var _enstr="<b>"+no.substring(_ewz+1)+"</b>";
                showno=_ststr+_cstr+_enstr;
            }
        }
    }
    return showno;
}

function downpdf(pdfurl,pdfid){

    jQuery("#endownpdf_"+pdfid).find(".searchright a span").text("准备文件...");
    jQuery("#endownpdf_"+pdfid).find(".searchright a").attr("href","javascript:void(0);");
    jQuery("#endownpdf_"+pdfid).find(".searchright a img").css("display","none");
    jQuery("#endownpdf_"+pdfid).find(".searchright a").css("text-decoration","none");

    if(urlhead!="/")
        pdfurl="pay/down/en";

    mydownpdf(pdfurl,pdfid,pdfinforesultEvent);
}
function pdfinforesultEvent(msg,pdfid){
    if(msg.type=="error"){
        jQuery("#endownpdf_"+pdfid).find(".searchright a span").text("暂无PDF");
    }else if(msg.type=="nofile"){
        jQuery("#endownpdf_"+pdfid).find(".searchright a span").text("暂无PDF");
    }else{
        jQuery("#endownpdf_"+pdfid).find(".searchright a span").text("点击下载！");
        jQuery("#endownpdf_"+pdfid).find(".searchright a img").css("display","inline");
        jQuery("#endownpdf_"+pdfid).find(".searchright a").css("text-decoration","underline");
        jQuery("#endownpdf_"+pdfid).find(".searchright a").attr("href",msg.url);
        jQuery("#endownpdf_"+pdfid).find(".searchright a").attr("target","_blank");
    }
}
